/**
 * The file for the Player class.
 */

package edu.gatech.information;

/**
 * The Class Player.
 * @author Hippie Mind Control
 * @version 1.0
 */
public class Player {
	
	/** The name. */
	public String name;
	
	/** The engineer skill. */
	public int pilotSkill, traderSkill, fighterSkill, engineerSkill, money;
	
	/** The planet location. */
	public Planet planetLocated;
	
	/** The ship */
	private Ship ship;
	
	/**
	 * Instantiates a new player.
	 *
	 * @param name the name
	 * @param pilotSkill the pilot skill
	 * @param traderSkill the trader skill
	 * @param fighterSkill the fighter skill
	 * @param engineerSkill the engineer skill
	 * @param planetLocated The planet that the player is at
	 */
	public Player(String name, int pilotSkill, int traderSkill, int fighterSkill, int engineerSkill, Planet planetLocated) {
		this.name = name;
		this.pilotSkill = pilotSkill;
		this.traderSkill = traderSkill;
		this.fighterSkill = fighterSkill;
		this.engineerSkill = engineerSkill;
		this.planetLocated = planetLocated;
		ship = new Ship();
		money = 500;
	}
	
	/**
	 * Empty player constructor.
	 */
	public Player() {
		ship = new Ship();
	}
	
	/**
	 * This checks to see if the planet the player selects is in range,
	 * meaning there is enough fuel on the ship to get there
	 * 
	 * @param planetClicked
	 * @return true
	 */
	public Boolean inRange(Planet planetClicked) {
		int distance = (int) Math.sqrt(Math.pow(planetClicked.getXPos()
			- planetLocated.getXPos(), 2)
			+ Math.pow(planetClicked.getYPos()
			- planetLocated.getYPos(), 2));
		if(ship.getCurrentFuel() * 25 >= distance) {
			if((ship.getCurrentFuel() - (distance / 25)) < 0){
				ship.setCurrentFuel(0);
			} else {
				ship.setCurrentFuel((ship.getCurrentFuel() - (distance / 25)));
			}
			return true;
		} else {
			return false;
		}
	}

	public int fuelToReach(Planet planetClicked)
	{
		int distance = (int) Math.sqrt(Math.pow(planetClicked.getXPos()
				- planetLocated.getXPos(), 2)
				+ Math.pow(planetClicked.getYPos()
				- planetLocated.getYPos(), 2));
		return distance / 25;
	}
	
	/**
	 * Gets the name.
	 *
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Sets the name.
	 *
	 * @param newName the new name
	 */
	public void setName(String newName) {
		name = newName;
	}
	
	public boolean addFuel(MarketPlace m) {
        if(this.getShip().canAddFuel()) {
                this.setMoney(this.getMoney() - m.getFinalPrice()[10]);
                this.getShip().setCurrentFuel(this.getShip().getCurrentFuel()+1);
                return true;
        }
        else {
                return false;
        }
	}
	
	
	 public boolean removeFuel(MarketPlace m) {
         if(this.getShip().getCurrentFuel() > 0)
         {
                 this.setMoney(this.getMoney() + m.getFinalPrice()[10]);
                 this.getShip().setCurrentFuel(this.getShip().getCurrentFuel()-1);
                 return true;
         }
         else
         {
                 return false;
         }
	 }
	
	/**
	 * Gets the pilot skill.
	 *
	 * @return the pilot skill
	 */
	public int getPilotSkill() {
		return pilotSkill;
	}
	
	/**
	 * Sets the pilot skill.
	 *
	 * @param newSkill the new pilot skill
	 */
	public void setPilotSkill(int newSkill) {
		pilotSkill = newSkill;
	}
	
	/**
	 * Gets the trader skill.
	 *
	 * @return the trader skill
	 */
	public int getTraderSkill() {
		return traderSkill;
	}
	
	/**
	 * Sets the trader skill.
	 *
	 * @param newSkill the new trader skill
	 */
	public void setTraderSkill(int newSkill) {
		traderSkill = newSkill;
	}

	/**
	 * Gets the fighter skill.
	 *
	 * @return the fighter skill
	 */
	public int getFighterSkill() {
		return fighterSkill;
	}
	
	/**
	 * Sets the fighter skill.
	 *
	 * @param newSkill the new fighter skill
	 */
	public void setFighterSkill(int newSkill) {
		fighterSkill = newSkill;
	}
	
	/**
	 * Gets the engineer skill.
	 *
	 * @return the engineer skill
	 */
	public int getEngineerSkill() {
		return engineerSkill;
	}
	
	/**
	 * Sets the engineer skill.
	 *
	 * @param newSkill the new engineer skill
	 */
	public void setEngineerSkill(int newSkill) {
		engineerSkill = newSkill;
	}
	
	/**
	 * Gets the planet located.
	 *
	 * @return the planet located
	 */
	public Planet getPlanetLocated() {
		return planetLocated;
	}
	
	/**
	 * Sets the planet located.
	 *
	 * @param planetLocated the new planet located
	 */
	public void setPlanetLocated(Planet planetLocated) {
		this.planetLocated = planetLocated;
	}
	
	/**
	 * Gets the ship type that the player has
	 * @return ship
	 */
	public Ship getShip() {
		return ship;
	}
	
	/**
	 * Gets the amount of money that the player has
	 * @return money
	 */
	public int getMoney() {
		return money;
	}
	
	/**
	 * Sets the amount of money that the player has
	 * @param money
	 */
	public void setMoney(int money) {
		this.money = money;
	}
	
	/**
	 * Converts player info to string.
	 *
	 * @return string of all the info
	 */
	public String toString() {
		String playerString = "";
		playerString += "Name: " + name + "\n";
		playerString += "Pilot Skill: " + pilotSkill + "\n";
		playerString += "Trader Skill: " + traderSkill + "\n";
		playerString += "Fighter Skill: " + fighterSkill + "\n";
		playerString += "Engineer Skill: " + engineerSkill + "\n";
		return playerString;
	}

	/**
	 * This is used to save the string of all the skills and money values of the player
	 * @return saveString
	 */
	public String saveString(){
		String saveString = "";
		saveString += name + "," + pilotSkill + "," + traderSkill + "," + fighterSkill + "," + engineerSkill + "," + money + "," + planetLocated.getName() + ",";
		saveString += ship.saveString();
		return saveString;
	}
	
	/**
	 * This is used when loading a previously saved game and reads information from a text file
	 * @param load
	 */
	public void loadString(String load) {
		try {
			String[] input = load.split(",");
			this.name = input[0];
			this.pilotSkill = Integer.parseInt(input[1]);
			this.traderSkill = Integer.parseInt(input[2]);
			this.fighterSkill = Integer.parseInt(input[3]);
			this.engineerSkill = Integer.parseInt(input[4]);
			this.money = Integer.parseInt(input[5]);
			
			// SHIP
			ship.setCargoBaySize(Integer.parseInt(input[7]));
			ship.setShipSpeed(Integer.parseInt(input[8]));
			ship.setFuelLevel(Integer.parseInt(input[9]));
			ship.setCurrentFuel(Integer.parseInt(input[10]));
			int[] tempCargo = new int[input.length - 11];
			for(int i = 11; i < input.length; i++) {
				tempCargo[i - 11] = Integer.parseInt(input[i]);
			}
			ship.setCargo(tempCargo);
		}
		catch(Exception e) {
			System.err.println(e);
		}
	}
}
